package org.hq;

import org.hq.utils.HttpUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class AppTests {

    @Autowired
    private HttpUtils http;

    @Test
    void test() throws Exception {
        long seconds = 8790;
        long hours = TimeUnit.SECONDS.toHours(seconds);
        long remainingSeconds = seconds - TimeUnit.HOURS.toSeconds(hours);
        long minutes = TimeUnit.SECONDS.toMinutes(remainingSeconds);
        remainingSeconds = remainingSeconds - TimeUnit.MINUTES.toSeconds(minutes);
        System.out.println(hours + "小时 " + minutes + "分钟 " + remainingSeconds + "秒");
        // 使用Duration将秒数转换为时间格式
        Duration duration = Duration.ofSeconds(seconds);
        // 将 Duration 转换为 LocalTime
        LocalTime localTime = LocalTime.MIN.plus(duration);
        // 将 LocalTime 转换为 Time
        Time time = Time.valueOf(localTime);
        // 输出 Time 对象
        System.out.println("Time: " + time);
        // 格式化时间
        String timeFormat = String.format("%02d:%02d:%02d",
                duration.toHours(),
                duration.toMinutesPart(),
                duration.toSecondsPart());

        // 输出时间格式
        System.out.println("Time Format: " + timeFormat);
    }

    /**
     * 获取指定网站的日期时间
     *
     * @param webUrl
     * @return
     * @author SHANHY
     * @date 2015年11月27日
     */
    private static String getWebsiteDatetime(String webUrl) {
        Time time = new Time(8790);
        try {
            URL url = new URL(webUrl);// 取得资源对象
            URLConnection uc = url.openConnection();// 生成连接对象
            uc.connect();// 发出连接
            long ld = uc.getDate();// 读取网站日期时间
            Date date = new Date(ld);// 转换为标准时间对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.CHINA);// 输出北京时间
            return sdf.format(date);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Test
    void getTime() throws Exception {

        String webUrl1 = "http://www.baidu.com";//百度
        String webUrl2 = "http://www.taobao.com";//淘宝
        String webUrl3 = "http://www.ntsc.ac.cn";//中国科学院国家授时中心
        String webUrl4 = "http://www.360.cn";//360
        String webUrl5 = "http://www.beijing-time.org";//beijing-time
        System.out.println(getWebsiteDatetime(webUrl1) + " [百度]");
        System.out.println(getWebsiteDatetime(webUrl2) + " [淘宝]");
        System.out.println(getWebsiteDatetime(webUrl3) + " [中国科学院国家授时中心]");
        System.out.println(getWebsiteDatetime(webUrl4) + " [360安全卫士]");
        System.out.println(getWebsiteDatetime(webUrl5) + " [beijing-time]");

    }

    @Test
    void contextLoads() {
        // 获取当前的日期和时间
        LocalDateTime now = LocalDateTime.now();
        // 定义日期和时间的格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss", Locale.CHINA);
        // 格式化当前的日期和时间
        String formattedDateTime = now.format(formatter);
        // 输出格式化后的日期和时间
        System.out.println("当前时间: " + formattedDateTime);
        // 提取年、月、日、时、分、秒等信息
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        int hour = now.getHour();
        int minute = now.getMinute();
        int second = now.getSecond();

        // 输出提取的信息
        System.out.println("年: " + year);
        System.out.println("月: " + month);
        System.out.println("日: " + day);
        System.out.println("时: " + hour);
        System.out.println("分: " + minute);
        System.out.println("秒: " + second);
    }

}
